package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.system.domain.OaProjectInitiation;
import com.ruoyi.system.service.IOaProjectInitiationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OaOtherMapper;
import com.ruoyi.system.domain.OaOther;
import com.ruoyi.system.service.IOaOtherService;

/**
 * 采购Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class OaOtherServiceImpl implements IOaOtherService {
    @Autowired
    private OaOtherMapper oaOtherMapper;
    @Autowired
    private IOaProjectInitiationService oaProjectInitiationService;
    /**
     * 查询采购
     *
     * @param id 采购主键
     * @return 采购
     */
    @Override
    public OaOther selectOaOtherById(Long id) {
        return oaOtherMapper.selectOaOtherById(id);
    }

    /**
     * 查询采购列表
     *
     * @param oaOther 采购
     * @return 采购
     */
    @Override
    public List<OaOther> selectOaOtherList(OaOther oaOther) {
        return oaOtherMapper.selectOaOtherList(oaOther);
    }
    @Override
    public List<OaOther> selectOaOtherList2(OaOther oaOther) {
        return oaOtherMapper.selectOaOtherList2(oaOther);
    }
    /**
     * 新增采购
     *
     * @param oaOther 采购
     * @return 结果
     */
    @Override
    public int insertOaOther(OaOther oaOther) {
        OaOther oaOther1 = new OaOther();
        oaOther1.setInitiationId(oaOther.getInitiationId());
        oaOther1.setType(oaOther.getType());
        oaOther1.setCategoryName(oaOther.getCategoryName());
        List<OaOther> oaOthers = oaOtherMapper.selectOaOtherList(oaOther1);
        if (!oaOthers.isEmpty()) {
            return 1;
        }
        if (  oaOther.getSpecificationModel().equals("")||oaOther.getSpecificationModel()==null){throw new RuntimeException("规格型号不能为空"); }
        int i = oaOtherMapper.insertOaOther(oaOther);
        //更新金额
        String initiationId = oaOther.getInitiationId();
        OaOther oaOther2 = new OaOther();
        oaOther2.setInitiationId(initiationId);
        List<OaOther> oaOthers1 = oaOtherMapper.selectOaOtherList(oaOther2);
        a(initiationId,oaOthers1);
        return i ;
    }

    public void a(String initiationId,List<OaOther> oaOthers1 ) {
        OaProjectInitiation oaProjectInitiation = oaProjectInitiationService.selectOaProjectInitiationById(Integer.parseInt(initiationId));
        BigDecimal paidAmount = BigDecimal.ZERO;
        for (OaOther oaOther : oaOthers1) {
            paidAmount = paidAmount.add(oaOther.getTotalBudget());
        }
        oaProjectInitiation.setTotalBudgetAmount(paidAmount);
        oaProjectInitiationService.updateOaProjectInitiation(oaProjectInitiation);
    }

    /**
     * 修改采购
     *
     * @param oaOther 采购
     * @return 结果
     */
    @Override
    public int updateOaOther(OaOther oaOther) {
        int i = oaOtherMapper.updateOaOther(oaOther);
        String initiationId = oaOther.getInitiationId();
        OaOther oaOther2 = new OaOther();
        oaOther2.setInitiationId(initiationId);
        List<OaOther> oaOthers1 = oaOtherMapper.selectOaOtherList(oaOther2);
        a(initiationId,oaOthers1);
        return i;
    }

    /**
     * 批量删除采购
     *
     * @param ids 需要删除的采购主键
     * @return 结果
     */
    @Override
    public int deleteOaOtherByIds(Long[] ids) {
        OaOther oaOther = null;
        for (Long id : ids) {
             oaOther = oaOtherMapper.selectOaOtherById(id);
        }
        int i = oaOtherMapper.deleteOaOtherByIds(ids);
        String initiationId = oaOther.getInitiationId();
        OaOther oaOther2 = new OaOther();
        oaOther2.setInitiationId(initiationId);
        List<OaOther> oaOthers1 = oaOtherMapper.selectOaOtherList(oaOther2);
        a(initiationId,oaOthers1);
        return i;
    }

    /**
     * 删除采购信息
     *
     * @param id 采购主键
     * @return 结果
     */
    @Override
    public int deleteOaOtherById(Long id) {
//        OaOther oaOther = oaOtherMapper.selectOaOtherById(id);
        int i = oaOtherMapper.deleteOaOtherById(id);
//        String initiationId = oaOther.getInitiationId();
//        OaOther oaOther2 = new OaOther();
//        oaOther2.setInitiationId(initiationId);
//        List<OaOther> oaOthers1 = oaOtherMapper.selectOaOtherList(oaOther2);
//        a(initiationId,oaOthers1);
        return i;
    }

    @Override
    public void delete(Integer id) {
        oaOtherMapper.delete(id);
    }
}
